//
//  XDTabBarViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/13/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "XDTabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"

#import "loginViewController.h"

#define btn_tag 10001
#define TABBARCOUNT 4

@interface XDTabBarViewController ()

@end

@implementation XDTabBarViewController

DEFINE_SINGLETON_FOR_CLASS(XDTabBarViewController)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    FirstViewController * vc1 = [[FirstViewController alloc] init];
    SecondViewController * vc2 = [[SecondViewController alloc] init];
    ThirdViewController * vc3 = [[ThirdViewController alloc] init];
    FourthViewController * vc4 = [[FourthViewController alloc] init];
    
    self.viewControllers = @[vc1,vc2,vc3,vc4];
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH, UI_TAB_BAR_HEIGHT)];
    _bgView.backgroundColor = UIColorFromRGB(0xf1f1f1);
    [self.tabBar addSubview:_bgView];
    for (int i = 0; i < TABBARCOUNT; i ++)
	{
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		button.frame = CGRectMake(i*UI_SCREEN_WIDTH/TABBARCOUNT,0,UI_SCREEN_WIDTH/TABBARCOUNT,UI_TAB_BAR_HEIGHT);
		button.enabled = i==DEFAULTSELECTINDEX?NO:YES;
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_notOn",i+1]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_notOn",i+1]] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_button%d_on",i+1]] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(chooseTabBar:) forControlEvents:UIControlEventTouchUpInside];
		button.adjustsImageWhenHighlighted = NO;
        button.tag = btn_tag+i;
        if (!button.enabled){
            [self setSelectedIndex:i];
            _beforeIndex = i;
            _presentIndex = i;
        }
		[_bgView addSubview:button];
	}

    self.reminder_IV = [[UIImageView alloc] initWithFrame:CGRectMake(160-25,5, 10, 10)];
    _reminder_IV.backgroundColor = [UIColor redColor];
    _reminder_IV.layer.cornerRadius = 5;
    _reminder_IV.layer.masksToBounds = YES;
//    [_bgView addSubview:_reminder_IV];
    _reminder_IV.hidden = YES;
    
    self.reminder_IV2 = [[UIImageView alloc] initWithFrame:CGRectMake(240-40,5, 8, 8)];
    _reminder_IV2.backgroundColor = [UIColor redColor];
    _reminder_IV2.layer.cornerRadius = 4;
    _reminder_IV2.layer.masksToBounds = YES;
    [_bgView addSubview:_reminder_IV2];
    _reminder_IV2.hidden = YES;
    
    if (ISLOGING) {
        
        [self getDataWithDict:@{@"CustomerID":[[NSUserDefaults standardUserDefaults] objectForKey:kMY_USER_ID] } api:API_GETYOUHUIQUAN];
    }

}

-(void)chooseTabBar:(UIButton *)sender
{
    if (sender.tag == btn_tag+3){
        //客服
        [self ContactCustomerService];
        return;
    }
    _presentIndex = sender.tag-btn_tag;
    if (_presentIndex == 1){
        if (!(ISLOGING)){
            loginViewController * loginVC = [[loginViewController alloc] init];
            loginVC.atype = 1;
            UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
            nav.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal;
            
            [self presentViewController:nav animated:YES completion:^{
                
            }];
            return;
        }
    }
    [self confirmSelectTabBar:_presentIndex];
    [self setSelectedIndex:_presentIndex];
    _beforeIndex = _presentIndex;
}

-(void)confirmSelectTabBar:(NSInteger)selectIndex
{
    for (UIButton * button in [_bgView subviews]){
        if ([button isKindOfClass:[UIButton class]]){
            button.enabled= button.tag==selectIndex+10001?NO:YES;
        }
    }
}

-(void)ContactCustomerService
{
    XDBaseViewController * selectViewController = (XDBaseViewController *)self.selectedViewController;
    UIActionSheet *alertAction = [[UIActionSheet alloc]initWithTitle:@"咨询投诉、摄像师应聘可拨打客服电话\n(9:20 - 22:00)" delegate:selectViewController cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"4008186060", nil];
    alertAction.tag = 908120;
    if (IOS7){
        [alertAction showInView:selectViewController.view];
    }else{
        [alertAction showFromTabBar:self.tabBar];
    }
}

//判断优惠券是否更新
-(void)getDataWithDict:(NSDictionary *)dict api:(NSString *)api
{
    if (![XDTools NetworkReachable]) {
        return;
    }
    
    __weak ASIHTTPRequest *request = [XDTools postRequestWithDict:dict API:api];
    [request setCompletionBlock:^{
        
        NSDictionary * tempDic = [XDTools  JSonFromString:[request responseString]];
        
        DDLOG(@"temdic = %@",tempDic);
        
        if([[tempDic objectForKey:@"status"]intValue] == 1){
            NSArray * arr = [tempDic objectForKey:@"data"];
            
            if (!ARRAY_IS_NOT_EMPTY(tempDic[@"data"])){
                _reminder_IV2.hidden = YES;
                return ;
            }
            //从本地读取上次请求下来的优惠券数量
            NSString * preNum = [[NSUserDefaults standardUserDefaults]objectForKey:@"VoucherNum"];
            
            NSString * currentNum = [NSString stringWithFormat:@"%d",arr.count];
            if (![preNum isEqualToString:currentNum]) {
                _reminder_IV2.hidden = NO;
                
            }else{
                //没有新优惠券
                _reminder_IV2.hidden = YES;
            }
            
        }else{
            
        }
    }];
    
    [request setFailedBlock:^{
        
    }];
    
    [request startAsynchronous];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


